using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Sec_scan漏洞扫描系统
{
    public partial class Sec_Scan_AddProject : Form
    {
        public Sec_Scan_AddProject()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //添加项目
            if(string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("数据不能为空！");
            }
            else
            {
                //执行项目添加
                ProjectAdd();
            }
        }
        /// <summary>
        /// 将项目信息添加到数据库
        /// </summary>
        public void ProjectAdd()
        {
            string path = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_LoopNumberApi.M1);
            SQLiteConnection connection = new SQLiteConnection(path);
            try
            {
                connection.Open();
                if(connection.State == ConnectionState.Open)
                {
                    Sec_Config.Sec_LoopNumber.NumberInfo(Sec_Api.Sec_LoopNumberApi.M1);
                    string number = Sec_Api.Sec_LoopNumberApi.LoopNum.ToString();
                    //需要把项目名称、编号、地址、类型、请求方式、代理模式 以缓存的方式进行传出
                    string sql = "INSERT INTO Sec_ProjectAddInfo (projectname, projectnumber, projectaddress, scantype, requesttype, scanproxx, addtime) VALUES('" + textBox1.Text + "', '" + number + "', '" + textBox2.Text + "', '" + comboBox1.Text + "', '" + comboBox2.Text + "', '" + comboBox3.Text + "', '" + DateTime.Now + "')";
                    SQLiteCommand command = new SQLiteCommand(sql, connection);
                    command.ExecuteReader();
                    //需要把项目数据传出
                    MessageBox.Show("项目数据添加成功！");
                    //指定触发一个窗体事件 然后进行关闭，在另外一个窗体事件中 获取这个数据
                    Close();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("访问地址不能为空，请输入网站域名！");
            }
            else
            {
                string Url = textBox2.Text;
                Sec_Api.Sec_Request_Service_Api.Url = Url;//传出网站域名
                try
                {
                    //这里不知道存在什么BUG 采用接口的方式不行 != null 也是不可以的
                    Sec_Service.Sec_Request_Service.RequestGet(Sec_Api.Sec_Request_Service_Api.A1);
                    //if(Sec_Api.Sec_Request_Service_Api.StatusOk.ToString() == "访问成功！")
                    //{
                    //    MessageBox.Show(Sec_Api.Sec_Request_Service_Api.StatusOk.ToString());
                    //}
                    //else
                    //{
                    //    MessageBox.Show(Sec_Api.Sec_Request_Service_Api.StatusExps.ToString());
                    //}
                    //采用缓存的方法
                    object ok = Sec_Config.Sec_CacheInfo.CacheValue("okinfo");
                    object no = Sec_Config.Sec_CacheInfo.CacheValue("okno");
                    if(ok.ToString().Length > 1)
                    {
                        MessageBox.Show("网站访问成功！");
                    }
                    else
                    {
                        MessageBox.Show("网站访问失败！");
                    }
                    Sec_Config.Sec_CacheInfo.CacheNull("okinfo");
                    Sec_Config.Sec_CacheInfo.CacheNull("okno");
                }
                catch(Exception)
                {
                    MessageBox.Show("网站访问错误！");
                }
            }
        }
    }
}
